import { ApprovalFlowModel } from '@/models' 

export default {
    data() {
        return {
            approvalFlowModel: new ApprovalFlowModel(),
            audit: false, // 是否是审核页
            belongItem: null,
            auditType: null,
            status: null,
            dialogTitle: '是否确定同意',
            formItems: [
                {
                    type: 'textarea',
                    model: 'content',
                    maxlength: 300,
                    autosize: { minRows: 6, maxRows: 10 },
                    label: '处理记录'
                }
            ],
            dialogRules: {
                // content: [{ required: true, message: '请填写反馈结果', trigger: ['blur', 'change'] }]
            },
            dialogForm: {},
            dialogVisible: false,
            approval: false,
            approvalForm: {
                tabulation: null,
                principal: null,
                higherPrincipal: null
            }
        }
    },
    created() {
        this._getQuery()
    },
    methods: {
        _getQuery() {
            this.audit = this.$route.query.audit || false // 负责人审核
            this.approval = this.$route.query.approval || false // 上级部门批复
            this.auditType = this.$route.query.type
            this.belongItem = this.$route.query.belongItem
        },
        async showDialog(status) {
            // 如果是上级批复，验证下表格验证通过不
            if (this.approval) {
                const res = await this.$refs['approvalForm'].handleValidate()
                if (!res) { return }
            }
            if (status === '1') {
                this.dialogTitle = '是否确定同意'
            } else {
                this.dialogTitle = '是否确定驳回'
            }
            this.status = status
            this.$refs['auditDialog'].show()
        },
        handleAudit(valida) {
            const api = this.approval ? 'approve' : 'audit'
            if (valida) {
                this.$store.commit('openBtnLoading')
                const data = {
                    number: this.number,
                    content: this.dialogForm.content,
                    status: this.status, // 审批状态：1 同意，2-驳回
                    belongItem: this.belongItem, // 所属项(1-养护预算、2-养护计划、3-计划实施、4-路产赔付、5-巡查计划)
                    type: this.auditType,
                    ...this.approvalForm // 上级批复时有值
                }
                this.approvalFlowModel[api](data).then(res => {
                    this.dialogVisible = false
                    this.$message({
                        type: 'success',
                        message: '操作成功！',
                        duration: 500,
                        onClose: () => {
                            this.$router.go(-1)
                        }
                    })
                }).finally(() => {
                    this.$store.commit('closeBtnLoading')
                })
            }
        }
    }
}
